<?php
class News_model extends CI_Model {
	function __construct() {
		parent::__construct();
	}
	
	function GetListForPaging($page, $limit, $CategoryID = null) {
		if(is_null($CategoryID)){
			$arr = array('TB_NEWS.Status' => 1);
		} else {
			$arr = array('TB_NEWS.Status' => 1, 'TB_NEWS.CategoryID' => $CategoryID);
		}
		$this->db	->select('NewsID, Title, Description, Avatar, Code, CreateDate')
					->from('TB_NEWS')
					->join('TB_ADMIN', 'TB_ADMIN.UserID = TB_NEWS.UserID')
					->where($arr)
					->order_by('NewsID', 'DESC')
					->limit($limit, $page);
		$query = $this->db->get();
		return $query->result();
	}
	
	function GetListCategory(){
		$query = $this->db->get('TB_CATEGORY');
		return $query->result();
	}
	
	function GetArrayCategory(){
		$result = $this->GetListCategory();
		$arr = array();
		if(!empty($result)){
			foreach($result as $key=>$value){
				$arr[$value->CategoryID] = $value->CategoryCode;
			}
			return $arr;
		} else {
			return false;
		}
	}
	
	function GetInfoByID($NewsID){
		$this->db->where(array('Status' => 1, 'NewsID' => $NewsID));
		$query = $this->db->get('TB_NEWS');
		$result = $query->result();
		return $result[0];
	}
	
	function CountByCategory($CategoryID){
		$this->db->where('CategoryID', $CategoryID);
		return $this->db->count_all_results('TB_NEWS');
	}
}
?>